package com.cc.persistent.controller.jpa;

import com.cc.persistent.model.jpa.Book;
import com.cc.persistent.service.jpa.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@RestController
@RequestMapping("jpa")
public class BookController {
    @Autowired
    BookService bookService;

    @GetMapping("/findAll")
    public void findAll() {
        PageRequest pageable = PageRequest.of(1, 3);
        Page<Book> page = bookService.getBookByPage(pageable);
        System.out.println("总页数:" + page.getTotalPages());
        System.out.println("总记录数: " + page.getTotalElements());
        System.out.println("查询结果:" + page.getContent());
        System.out.println("当前页数:" + (page.getNumber() + 1));
        System.out.println("当前页记录数: " + page.getNumberOfElements());
        System.out.println("毎页记录数:" + page.getSize());
    }

    @GetMapping("/search")
    public void search() {
        List<Book> bs1 = bookService.getBookByIdAndAuthor("魯迅", 7);
        List<Book> bs2 = bookService.getBooksByAuthorStartingWith("昊");
        List<Book> bs3 = bookService.getBooksByIdAndName("西", 8);
        List<Book> bs4 = bookService.getBooksByPriceGreaterThan(30f);
        Book b = bookService.getMaxIdBook();
        System.out.println("bs1:" + bs1);
        System.out.println("bs2:" + bs2);
        System.out.println("bs3:" + bs3);
        System.out.println("bs4:" + bs4);
        System.out.println("b:" + b);
    }

    @GetMapping("/save")
    public void save() {
        Book book = new Book();
        book.setAuthor("魯迅");
        book.setName("吶喊");
        book.setPrice(23f);
        bookService.addBook(book);
    }
}    